#!/usr/bin/env bash

############################################################################
#
# 该脚本基于社区其它公开代码改造完成，非本人原创，允许以MIT协议进行传播
#
# 把该脚本下载到本地，必须以本地文件的方式执行，因为脚本中有检测当前进程参数的代码：
#
#   wget  https://gitee.com/tay3223/biubiubiu/raw/master/centos7-install-openvpn-v2.sh -O openvpn.sh
#
# 如需帮助可以发邮件给作者：tianye3223@gmail.com
#
############################################################################

VIRTUAL_NETWORK_SEGMENT="10.8.44.0"

#############################
# 检测使用“sh”而不是 bash 运行脚本的 Debian 用户
#############################
if readlink /proc/$$/exe | grep -q "dash"; then
    echo '此安装程序需要使用“bash”而不是“sh”运行。'
    exit
fi

#############################
# 丢弃标准输入，从包含换行符的单行运行时需要
#############################
read -N 999999 -t 0.001

#############################
# 检测 OpenVZ 6
#############################
if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
    echo "系统正在运行与此安装程序不兼容的旧内核。"
    exit
fi

#############################
# 检测操作系统
# os_version 变量并不总是在使用，但为了方便而保留在这里
#############################
if grep -qs "ubuntu" /etc/os-release; then
    os="ubuntu"
    os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
    group_name="nogroup"
elif [[ -e /etc/debian_version ]]; then
    os="debian"
    os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
    group_name="nogroup"
elif [[ -e /etc/almalinux-release || -e /etc/rocky-release || -e /etc/centos-release ]]; then
    os="centos"
    os_version=$(grep -shoE '[0-9]+' /etc/almalinux-release /etc/rocky-release /etc/centos-release | head -1)
    group_name="nobody"
elif [[ -e /etc/fedora-release ]]; then
    os="fedora"
    os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
    group_name="nobody"
else
    echo "此安装程序似乎在不受支持的发行版上运行。支持的发行版包括 Ubuntu、Debian、AlmaLinux、Rocky Linux、CentOS 和 Fedora。"
    exit
fi

#############################
# 检测版本是否太旧
#############################
if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then
    echo "使用此安装程序需要 Ubuntu 18.04 或更高版本。此版本的 Ubuntu 太旧且不受支持。"
    exit
fi

if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then
    echo "使用此安装程序需要 Debian 9 或更高版本。此版本的 Debian 太旧且不受支持。"
    exit
fi

if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then
    echo "使用此安装程序需要 CentOS 7 或更高版本。此版本的 CentOS 太旧且不受支持。"
    exit
fi

#############################
# 检测 PATH 不包含 sbin 目录的环境
#############################
if ! grep -q sbin <<<"$PATH"; then
    echo '$PATH 不包括 sbin。尝试使用 "su -" 代替 "su"。'
    exit
fi

if [[ "$EUID" -ne 0 ]]; then
    echo "此安装程序需要以超级用户权限运行。"
    exit
fi

if [[ ! -e /dev/net/tun ]] || ! (exec 7<>/dev/net/tun) 2>/dev/null; then
    echo "系统没有可用的 TUN 设备。在运行此安装程序之前需要启用 TUN。"
    exit
fi

#############################
# 创建客户单证书
#############################
new_client() {
    # 生成自定义 client.ovpn
    {
        cat /etc/openvpn/server/client-common.txt
        echo "<ca>"
        cat /etc/openvpn/server/easy-rsa/pki/ca.crt
        echo "</ca>"
        echo "<cert>"
        sed -ne '/BEGIN CERTIFICATE/,$ p' /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt
        echo "</cert>"
        echo "<key>"
        cat /etc/openvpn/server/easy-rsa/pki/private/"$client".key
        echo "</key>"
        echo "<tls-crypt>"
        sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.key
        echo "</tls-crypt>"
    } >~/"$client".ovpn
}

#############################
# 安装OpenVPN服务端
#############################
if [[ ! -e /etc/openvpn/server/server.conf ]]; then
    # 检测一些既没有安装 wget 也没有安装 curl 的 Debian 最小设置
    if ! hash wget 2>/dev/null && ! hash curl 2>/dev/null; then
        echo "使用此安装程序需要 Wget。"
        read -n1 -r -p "按任意键安装 Wget 并继续..."
        apt-get update
        apt-get install -y wget
    fi
    clear
    echo '欢迎使用此 OpenVPN 安装程序！'
    # 如果系统只有一个 IPv4，它会被自动选择。否则，询问用户
    if [[ $(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}') -eq 1 ]]; then
        ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}')
    else
        number_of_ip=$(ip -4 addr | grep inet | grep -vEc '127(\.[0-9]{1,3}){3}')
        echo
        echo "应该使用哪个 IPv4 地址？"
        ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | nl -s ') '
        read -p "IPv4 address [1]: " ip_number
        until [[ -z "$ip_number" || "$ip_number" =~ ^[0-9]+$ && "$ip_number" -le "$number_of_ip" ]]; do
            echo "$ip_number: invalid selection."
            read -p "IPv4 address [1]: " ip_number
        done
        [[ -z "$ip_number" ]] && ip_number="1"
        ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "$ip_number"p)
    fi
    # 如果 ip 是私有 IP 地址，则服务器必须在 NAT 之后
    if echo "$ip" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
        echo
        echo "此服务器位于 NAT 之后。什么是公共 IPv4 地址或主机名？"
        # 获取公共 IP 并使用 grep 进行清理
        get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<<"$(wget -T 10 -t 1 -4qO- "http://ip1.dynupdate.no-ip.com/" || curl -m 10 -4Ls "http://ip1.dynupdate.no-ip.com/")")
        read -p "Public IPv4 address / hostname [$get_public_ip]: " public_ip
        # 如果 checkip 服务不可用且用户未提供输入，请再次询问
        until [[ -n "$get_public_ip" || -n "$public_ip" ]]; do
            echo "Invalid input."
            read -p "Public IPv4 address / hostname: " public_ip
        done
        [[ -z "$public_ip" ]] && public_ip="$get_public_ip"
    fi
    # 如果系统只有一个 IPv6，它会被自动选择
    if [[ $(ip -6 addr | grep -c 'inet6 [23]') -eq 1 ]]; then
        ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}')
    fi
    # 如果系统有多个 IPv6，请用户选择一个
    if [[ $(ip -6 addr | grep -c 'inet6 [23]') -gt 1 ]]; then
        number_of_ip6=$(ip -6 addr | grep -c 'inet6 [23]')
        echo
        echo "Which IPv6 address should be used?"
        ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | nl -s ') '
        read -p "IPv6 address [1]: " ip6_number
        until [[ -z "$ip6_number" || "$ip6_number" =~ ^[0-9]+$ && "$ip6_number" -le "$number_of_ip6" ]]; do
            echo "$ip6_number: invalid selection."
            read -p "IPv6 address [1]: " ip6_number
        done
        [[ -z "$ip6_number" ]] && ip6_number="1"
        ip6=$(ip -6 addr | grep 'inet6 [23]' | cut -d '/' -f 1 | grep -oE '([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}' | sed -n "$ip6_number"p)
    fi
    echo
    echo "OpenVPN 应该使用哪种协议？"
    echo "   1) UDP (推荐的)"
    echo "   2) TCP"
    read -p "Protocol [1]: " protocol
    until [[ -z "$protocol" || "$protocol" =~ ^[12]$ ]]; do
        echo "$protocol: invalid selection."
        read -p "Protocol [1]: " protocol
    done
    case "$protocol" in
    1 | "")
        protocol=udp
        ;;
    2)
        protocol=tcp
        ;;
    esac
    echo
    echo "OpenVPN 应该侦听哪个端口？"
    read -p "Port [1194]: " port
    until [[ -z "$port" || "$port" =~ ^[0-9]+$ && "$port" -le 65535 ]]; do
        echo "$port: invalid port."
        read -p "Port [1194]: " port
    done
    [[ -z "$port" ]] && port="1194"
    echo
    echo "为客户端选择 DNS 服务器："
    echo "   1) 当前系统的DNS"
    echo "   2) Google"
    echo "   3) 1.1.1.1"
    echo "   4) OpenDNS"
    echo "   5) Quad9"
    echo "   6) AdGuard"
    read -p "DNS服务器 [1]: " dns
    until [[ -z "$dns" || "$dns" =~ ^[1-6]$ ]]; do
        echo "$dns: invalid selection."
        read -p "DNS server [1]: " dns
    done
    echo
    echo "输入第一个客户端的名称："
    read -p "Name [client]: " unsanitized_client
    # 允许一组有限的字符以避免冲突
    client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
    [[ -z "$client" ]] && client="client"
    echo
    echo "OpenVPN 安装已准备好开始。"
    # 如果 firewalld 或 iptables 不可用，请安装防火墙
    if ! systemctl is-active --quiet firewalld.service && ! hash iptables 2>/dev/null; then
        if [[ "$os" == "centos" || "$os" == "fedora" ]]; then
            firewall="firewalld"
            # 我们不想默默地启用firewalld，所以我们给出了一个微妙的警告
            # 如果用户继续，firewalld 将在安装过程中安装并启用
            echo "管理路由表所需的 firewalld 也将被安装。"
        elif [[ "$os" == "debian" || "$os" == "ubuntu" ]]; then
            # iptables 的侵入性比 firewalld 小，因此不会发出警告
            firewall="iptables"
        fi
    fi
    read -n1 -r -p "按任意键继续..."
    # 如果在容器内运行，请禁用 LimitNPROC 以防止冲突
    if systemd-detect-virt -cq; then
        mkdir /etc/systemd/system/openvpn-server@server.service.d/ 2>/dev/null
        echo "[Service]
LimitNPROC=infinity" >/etc/systemd/system/openvpn-server@server.service.d/disable-limitnproc.conf
    fi
    if [[ "$os" = "debian" || "$os" = "ubuntu" ]]; then
        apt-get update
        apt-get install -y openvpn openssl ca-certificates $firewall
    elif [[ "$os" = "centos" ]]; then
        yum install -y epel-release
        yum install -y openvpn openssl ca-certificates tar $firewall
    else
        # 否则，操作系统必须是 Fedora
        dnf install -y openvpn openssl ca-certificates tar $firewall
    fi
    # 如果firewalld刚刚安装，启用它
    if [[ "$firewall" == "firewalld" ]]; then
        systemctl enable --now firewalld.service
    fi
    # 获取 easy-rsa
    #easy_rsa_url='https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz'
    easy_rsa_url='https://osoc.oss-cn-shanghai.aliyuncs.com/bao/easyrsa/EasyRSA-3.0.8.tgz'
    mkdir -p /etc/openvpn/server/easy-rsa/
    { wget -qO- "$easy_rsa_url" 2>/dev/null || curl -sL "$easy_rsa_url"; } | tar xz -C /etc/openvpn/server/easy-rsa/ --strip-components 1
    chown -R root:root /etc/openvpn/server/easy-rsa/
    cd /etc/openvpn/server/easy-rsa/
    # 创建 PKI，设置 CA 以及服务器和客户端证书
    ./easyrsa init-pki
    ./easyrsa --batch build-ca nopass
    EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-server-full server nopass
    EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "$client" nopass
    EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
    # 移动我们需要的东西
    cp pki/ca.crt pki/private/ca.key pki/issued/server.crt pki/private/server.key pki/crl.pem /etc/openvpn/server
    # 每个客户端连接都会读取 CRL，而 OpenVPN 被丢弃给任何人
    chown nobody:"$group_name" /etc/openvpn/server/crl.pem
    # 如果目录中没有 +x，OpenVPN 无法在 CRL 文件上运行 stat()
    chmod o+x /etc/openvpn/server/
    # 为 tls-crypt 生成密钥
    openvpn --genkey --secret /etc/openvpn/server/tc.key
    # 使用预定义的 ffdhe2048 组创建 DH 参数文件
    echo '-----BEGIN DH PARAMETERS-----
MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz
+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a
87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7
YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi
7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD
ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg==
-----END DH PARAMETERS-----' >/etc/openvpn/server/dh.pem
    # 生成 server.conf
    echo "local $ip
port $port
proto $protocol
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
auth SHA512
tls-crypt tc.key
topology subnet
server $VIRTUAL_NETWORK_SEGMENT 255.255.255.0" >/etc/openvpn/server/server.conf
    # IPv6
    if [[ -z "$ip6" ]]; then
        echo 'push "redirect-gateway def1 bypass-dhcp"' >>/etc/openvpn/server/server.conf
    else
        echo 'server-ipv6 fddd:1194:1194:1194::/64' >>/etc/openvpn/server/server.conf
        echo 'push "redirect-gateway def1 ipv6 bypass-dhcp"' >>/etc/openvpn/server/server.conf
    fi
    echo 'ifconfig-pool-persist ipp.txt' >>/etc/openvpn/server/server.conf
    # DNS
    case "$dns" in
    1 | "")
        # 找到正确的resolv.conf
        # 运行 systemd-resolved 的系统需要
        if grep -q '^nameserver 127.0.0.53' "/etc/resolv.conf"; then
            resolv_conf="/run/systemd/resolve/resolv.conf"
        else
            resolv_conf="/etc/resolv.conf"
        fi
        # 从 resolv.conf 获取解析器并将它们用于 OpenVPN
        grep -v '^#\|^;' "$resolv_conf" | grep '^nameserver' | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | while read line; do
            echo "push \"dhcp-option DNS $line\"" >>/etc/openvpn/server/server.conf
        done
        ;;
    2)
        echo 'push "dhcp-option DNS 8.8.8.8"' >>/etc/openvpn/server/server.conf
        echo 'push "dhcp-option DNS 8.8.4.4"' >>/etc/openvpn/server/server.conf
        ;;
    3)
        echo 'push "dhcp-option DNS 1.1.1.1"' >>/etc/openvpn/server/server.conf
        echo 'push "dhcp-option DNS 1.0.0.1"' >>/etc/openvpn/server/server.conf
        ;;
    4)
        echo 'push "dhcp-option DNS 208.67.222.222"' >>/etc/openvpn/server/server.conf
        echo 'push "dhcp-option DNS 208.67.220.220"' >>/etc/openvpn/server/server.conf
        ;;
    5)
        echo 'push "dhcp-option DNS 9.9.9.9"' >>/etc/openvpn/server/server.conf
        echo 'push "dhcp-option DNS 149.112.112.112"' >>/etc/openvpn/server/server.conf
        ;;
    6)
        echo 'push "dhcp-option DNS 94.140.14.14"' >>/etc/openvpn/server/server.conf
        echo 'push "dhcp-option DNS 94.140.15.15"' >>/etc/openvpn/server/server.conf
        ;;
    esac
    echo "keepalive 10 120
cipher AES-256-CBC
user nobody
group $group_name
persist-key
persist-tun
verb 3
crl-verify crl.pem" >>/etc/openvpn/server/server.conf
    if [[ "$protocol" = "udp" ]]; then
        echo "explicit-exit-notify" >>/etc/openvpn/server/server.conf
    fi
    # 为系统启用net.ipv4.ip_forward
    echo 'net.ipv4.ip_forward=1' >/etc/sysctl.d/99-openvpn-forward.conf
    # 无需等待重启或服务重启即可启用
    echo 1 >/proc/sys/net/ipv4/ip_forward
    if [[ -n "$ip6" ]]; then
        # 为系统启用net.ipv6.conf.all.forwarding
        echo "net.ipv6.conf.all.forwarding=1" >>/etc/sysctl.d/99-openvpn-forward.conf
        # 无需等待重启或服务重启即可启用
        echo 1 >/proc/sys/net/ipv6/conf/all/forwarding
    fi
    if systemctl is-active --quiet firewalld.service; then
        # 使用永久和非永久规则来避免防火墙
        # 重新加载。
        # 我们不使用 --add-service=openvpn 因为它只适用于
        # 默认端口和协议。
        firewall-cmd --add-port="$port"/"$protocol"
        firewall-cmd --zone=trusted --add-source=$VIRTUAL_NETWORK_SEGMENT/24
        firewall-cmd --permanent --add-port="$port"/"$protocol"
        firewall-cmd --permanent --zone=trusted --add-source=$VIRTUAL_NETWORK_SEGMENT/24
        # 为 VPN 子网设置 NAT
        firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to "$ip"
        firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to "$ip"
        if [[ -n "$ip6" ]]; then
            firewall-cmd --zone=trusted --add-source=fddd:1194:1194:1194::/64
            firewall-cmd --permanent --zone=trusted --add-source=fddd:1194:1194:1194::/64
            firewall-cmd --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
            firewall-cmd --permanent --direct --add-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
        fi
    else
        # 创建一个服务来设置持久的 iptables 规则
        iptables_path=$(command -v iptables)
        ip6tables_path=$(command -v ip6tables)
        # nf_tables 在 OVZ 内核中不作为标准提供。所以使用 iptables-legacy
        # 如果我们在 OVZ 中，则可以使用 nf_tables 后端和 iptables-legacy。
        if [[ $(systemd-detect-virt) == "openvz" ]] && readlink -f "$(command -v iptables)" | grep -q "nft" && hash iptables-legacy 2>/dev/null; then
            iptables_path=$(command -v iptables-legacy)
            ip6tables_path=$(command -v ip6tables-legacy)
        fi
        echo "[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=$iptables_path -t nat -A POSTROUTING -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to $ip
ExecStart=$iptables_path -I INPUT -p $protocol --dport $port -j ACCEPT
ExecStart=$iptables_path -I FORWARD -s $VIRTUAL_NETWORK_SEGMENT/24 -j ACCEPT
ExecStart=$iptables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
ExecStop=$iptables_path -t nat -D POSTROUTING -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to $ip
ExecStop=$iptables_path -D INPUT -p $protocol --dport $port -j ACCEPT
ExecStop=$iptables_path -D FORWARD -s $VIRTUAL_NETWORK_SEGMENT/24 -j ACCEPT
ExecStop=$iptables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" >/etc/systemd/system/openvpn-iptables.service
        if [[ -n "$ip6" ]]; then
            echo "ExecStart=$ip6tables_path -t nat -A POSTROUTING -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to $ip6
ExecStart=$ip6tables_path -I FORWARD -s fddd:1194:1194:1194::/64 -j ACCEPT
ExecStart=$ip6tables_path -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
ExecStop=$ip6tables_path -t nat -D POSTROUTING -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to $ip6
ExecStop=$ip6tables_path -D FORWARD -s fddd:1194:1194:1194::/64 -j ACCEPT
ExecStop=$ip6tables_path -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" >>/etc/systemd/system/openvpn-iptables.service
        fi
        echo "RemainAfterExit=yes
[Install]
WantedBy=multi-user.target" >>/etc/systemd/system/openvpn-iptables.service
        systemctl enable --now openvpn-iptables.service
    fi
    # 如果启用了 SELinux 并选择了自定义端口，我们需要这个
    if sestatus 2>/dev/null | grep "Current mode" | grep -q "enforcing" && [[ "$port" != 1194 ]]; then
        # 如果还没有安装 semanage
        if ! hash semanage 2>/dev/null; then
            if [[ "$os_version" -eq 7 ]]; then
                # Centos 7
                yum install -y policycoreutils-python
            else
                # CentOS 8 or Fedora
                dnf install -y policycoreutils-python-utils
            fi
        fi
        semanage port -a -t openvpn_port_t -p "$protocol" "$port"
    fi
    # 如果服务器在 NAT 之后，请使用正确的 IP 地址
    [[ -n "$public_ip" ]] && ip="$public_ip"
    # client-common.txt 已创建，因此我们有一个模板可以稍后添加更多用户
    echo "client
dev tun
proto $protocol
remote $ip $port
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
auth SHA512
cipher AES-256-CBC
ignore-unknown-option block-outside-dns
block-outside-dns
verb 3" >/etc/openvpn/server/client-common.txt
    # 启用并启动 OpenVPN 服务
    systemctl enable --now openvpn-server@server.service
    # 生成自定义 client.ovpn
    new_client
    echo
    echo "Finished!"
    echo
    echo "客户端配置可用于：" ~/"$client.ovpn"
    echo "可以通过再次运行此脚本来添加新客户端。"
else
    clear
    echo "OpenVPN 已经安装。"
    echo
    echo "选择一个选项："
    echo "   1) 添加新客户"
    echo "   2) 撤销现有客户"
    echo "   3) 删除 OpenVPN 服务端"
    echo "   4) Exit"
    read -p "Option: " option
    until [[ "$option" =~ ^[1-4]$ ]]; do
        echo "$option: invalid selection."
        read -p "Option: " option
    done
    case "$option" in
    1)
        echo
        echo "为客户端提供一个名称："
        read -p "Name: " unsanitized_client
        client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
        while [[ -z "$client" || -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]]; do
            echo "$client: invalid name."
            read -p "Name: " unsanitized_client
            client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<<"$unsanitized_client")
        done
        cd /etc/openvpn/server/easy-rsa/
        EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full "$client" nopass
        # 生成自定义 client.ovpn
        new_client
        echo
        echo "$client added. Configuration available in:" ~/"$client.ovpn"
        exit
        ;;
    2)
        # This option could be documented a bit better and maybe even be simplified
        # ...but what can I say, I want some sleep too
        number_of_clients=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep -c "^V")
        if [[ "$number_of_clients" = 0 ]]; then
            echo
            echo "没有现有客户！"
            exit
        fi
        echo
        echo "选择要撤销的客户："
        tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '
        read -p "Client: " client_number
        until [[ "$client_number" =~ ^[0-9]+$ && "$client_number" -le "$number_of_clients" ]]; do
            echo "$client_number: invalid selection."
            read -p "Client: " client_number
        done
        client=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$client_number"p)
        echo
        read -p "Confirm $client revocation? [y/N]: " revoke
        until [[ "$revoke" =~ ^[yYnN]*$ ]]; do
            echo "$revoke: invalid selection."
            read -p "Confirm $client revocation? [y/N]: " revoke
        done
        if [[ "$revoke" =~ ^[yY]$ ]]; then
            cd /etc/openvpn/server/easy-rsa/
            ./easyrsa --batch revoke "$client"
            EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
            rm -f /etc/openvpn/server/crl.pem
            cp /etc/openvpn/server/easy-rsa/pki/crl.pem /etc/openvpn/server/crl.pem
            # 当 OpenVPN 被放弃时，每个客户端连接都会读取 CRL
            chown nobody:"$group_name" /etc/openvpn/server/crl.pem
            echo
            echo "$client revoked!"
        else
            echo
            echo "$client revocation aborted!"
        fi
        exit
        ;;
    3)
        echo
        read -p "确认删除 OpenVPN？ [y/N]: " remove
        until [[ "$remove" =~ ^[yYnN]*$ ]]; do
            echo "$remove: invalid selection."
            read -p "确认删除 OpenVPN？ [y/N]: " remove
        done
        if [[ "$remove" =~ ^[yY]$ ]]; then
            port=$(grep '^port ' /etc/openvpn/server/server.conf | cut -d " " -f 2)
            protocol=$(grep '^proto ' /etc/openvpn/server/server.conf | cut -d " " -f 2)
            if systemctl is-active --quiet firewalld.service; then
                ip=$(firewall-cmd --direct --get-rules ipv4 nat POSTROUTING | grep '\-s '"$VIRTUAL_NETWORK_SEGMENT"'/24 '"'"'!'"'"' -d '"$VIRTUAL_NETWORK_SEGMENT"'/24' | grep -oE '[^ ]+$')
                # 使用永久和非永久规则来避免 firewalld 重新加载。
                firewall-cmd --remove-port="$port"/"$protocol"
                firewall-cmd --zone=trusted --remove-source=$VIRTUAL_NETWORK_SEGMENT/24
                firewall-cmd --permanent --remove-port="$port"/"$protocol"
                firewall-cmd --permanent --zone=trusted --remove-source=$VIRTUAL_NETWORK_SEGMENT/24
                firewall-cmd --direct --remove-rule ipv4 nat POSTROUTING 0 -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to "$ip"
                firewall-cmd --permanent --direct --remove-rule ipv4 nat POSTROUTING 0 -s $VIRTUAL_NETWORK_SEGMENT/24 ! -d $VIRTUAL_NETWORK_SEGMENT/24 -j SNAT --to "$ip"
                if grep -qs "server-ipv6" /etc/openvpn/server/server.conf; then
                    ip6=$(firewall-cmd --direct --get-rules ipv6 nat POSTROUTING | grep '\-s fddd:1194:1194:1194::/64 '"'"'!'"'"' -d fddd:1194:1194:1194::/64' | grep -oE '[^ ]+$')
                    firewall-cmd --zone=trusted --remove-source=fddd:1194:1194:1194::/64
                    firewall-cmd --permanent --zone=trusted --remove-source=fddd:1194:1194:1194::/64
                    firewall-cmd --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
                    firewall-cmd --permanent --direct --remove-rule ipv6 nat POSTROUTING 0 -s fddd:1194:1194:1194::/64 ! -d fddd:1194:1194:1194::/64 -j SNAT --to "$ip6"
                fi
            else
                systemctl disable --now openvpn-iptables.service
                rm -f /etc/systemd/system/openvpn-iptables.service
            fi
            if sestatus 2>/dev/null | grep "Current mode" | grep -q "enforcing" && [[ "$port" != 1194 ]]; then
                semanage port -d -t openvpn_port_t -p "$protocol" "$port"
            fi
            systemctl disable --now openvpn-server@server.service
            rm -f /etc/systemd/system/openvpn-server@server.service.d/disable-limitnproc.conf
            rm -f /etc/sysctl.d/99-openvpn-forward.conf
            if [[ "$os" = "debian" || "$os" = "ubuntu" ]]; then
                rm -rf /etc/openvpn/server
                apt-get remove --purge -y openvpn
            else
                # 否则，操作系统必须是 CentOS 或 Fedora
                yum remove -y openvpn
                rm -rf /etc/openvpn/server
            fi
            echo
            echo "OpenVPN 已移除！"
        else
            echo
            echo "OpenVPN 删除中止！"
        fi
        exit
        ;;
    4)
        exit
        ;;
    esac
fi
